package com.zyb.learning.dataStructure.queue;

import java.util.Scanner;

/**
 * @ClassName CircleArrayQueue
 * @Author zhaoyb
 * @Date 2023/9/28
 * @Version 1.0
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        System.out.println("模拟环形队列~~~~~~~");
        CircleArrayQueue arrayQueue = new CircleArrayQueue(4);
        char key = ' ';//接收输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出队列");
            System.out.println("g(get):从队列取出数据"); //出队
            System.out.println("a(add):添加数据到队列"); //入队
            System.out.println("h(head):查看队列头数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'g':
                    try {
                        int result = arrayQueue.getQueue();
                        System.out.println("取出的数据是：" + result);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'a':
                    System.out.println("请输入要添加的值");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'h':
                    try {
                        int headValue = arrayQueue.headQueue();
                        System.out.println("队列头数据是：" + headValue);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}

class CircleArrayQueue {
    private int maxSize; //数组最大容量
    private int front; //头指针
    private int rear; //尾指针
    private int[] arr; //模拟环形队列的数组

    public CircleArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        front = 0;
        rear = 0;
        arr = new int[maxSize];
    }

    //是否满
    public boolean isFull() {
        return (rear + 1) % maxSize == front; //仔细想想为什么这么做
    }

    /**
     * 判断队列是否空
     */
    public boolean isEmpty() {
        return rear == front;
    }

    /**
     * 添加元素，入队
     */
    public void addQueue(int n) {
        //判断是否满
        if (isFull()) {
            System.out.println("队列已经满了，不能添加");
            return;
        }
        arr[rear] = n;
        rear = (rear + 1) % maxSize; //入队,仔细想想为什么这么做
    }

    /**
     * 出队列
     *
     * @return
     */
    public int getQueue() {
        //判空
        if (isEmpty()) {
            throw new IllegalArgumentException("队列已经空了，不能取数据");
        }

        //1.先把front对应的值保存在临时变量
        int value = arr[front];
        //2. 讲front后移
        front = (front + 1) % maxSize;
        //3.将临时保存的变量返回
        return value;
    }

    //显示队列的所有数据
    public void showQueue() {
        //判空
        if (isEmpty()) {
            System.out.println("队列是空，没有数据！");
            return;
        }
        //思路：从front开始，遍历maxsize个元素
        for (int i = front; i < front + usefulSize(); i++) {
            System.out.printf("arr[%d] = %d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    //求出有效数据个数
    public int usefulSize() {
        return (rear + maxSize - front) % maxSize;
    }


    //显示队列的头数据，不出队列，仅仅展示
    public int headQueue() {
        if (isEmpty()) {
            throw new IllegalArgumentException("队列已经空了，获取不到数据");
        }
        return arr[front];
    }

}
